<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="患者" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="patientId">
              <a-select v-model="model.patientId" placeholder="请选择患者" @change="changePat">
                <a-select-option :value="i.id" v-for="(i, o) in patList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="医生" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="doctorId">
              <a-select v-model="model.doctorId" placeholder="请选择医生" :disabled="!model.patientId">
                <a-select-option :value="i.id" v-for="(i, o) in docList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="治疗日期" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="healDate">
              <j-date placeholder="请选择治疗日期" dateFormat="YYYY-MM-DD HH:mm:ss" :showTime="true" v-model="model.healDate"
              style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="疾病名称分类" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="illnessClass">
              <a-input v-model="model.illnessClass" placeholder="请输入疾病名称分类"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="治疗描述" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="healDesc">
              <a-textarea v-model="model.healDesc" rows="4" placeholder="请输入治疗描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="诊断结果" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="diagnosis">
              <a-textarea v-model="model.diagnosis" rows="4" placeholder="请输入诊断结果" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="处方信息" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="prescription">
              <a-input v-model="model.prescription" placeholder="请输入处方信息"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="问诊时间" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="healTime">
              <j-date placeholder="请选择问诊时间" dateFormat="YYYY-MM-DD HH:mm:ss" :showTime="true" v-model="model.healTime"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否需要复诊" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isVisit">
              <j-dict-select-tag v-model="model.isVisit" placeholder="请选择是否需要复诊" dictCode="SFXYFZ"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="复诊时间" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visitDate">
              <j-date placeholder="请选择复诊时间" dateFormat="YYYY-MM-DD HH:mm:ss" :showTime="true" v-model="model.visitDate"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WzHealRecordForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      patList: [],
      docList: [],
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        patientId: [{ required: true, message: '请选择患者!' }],
        doctorId: [{ required: true, message: '请选择医生!' }],
        healDate: [{ required: true, message: '请选择治疗日期!' }],
        illnessClass: [{ required: true, message: '请输入疾病名称分类!' }],
        healDesc: [{ required: true, message: '请输入治疗描述!' }],
        diagnosis: [{ required: true, message: '请输入诊断结果!' }],
        prescription: [{ required: true, message: '请输入处方信息!' }],
        healTime: [{ required: true, message: '请选择问诊时间!' }],
        isVisit: [{ required: true, message: '请选择是否需要复诊!' }],
        visitDate: [{ required: true, message: '请选择复诊时间!' }],
      },
      url: {
        add: "/wz/heal/wzHealRecord/add",
        edit: "/wz/heal/wzHealRecord/edit",
        queryById: "/wz/heal/wzHealRecord/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    getAction('/wz/patient/wzPatientInfo/list', { pageNo: 1, pageSize: 999 }).then(res => {
      this.patList = res.result.records;
    })
  },
  methods: {
    changePat(val) {
      const orgId = this.patList.find(i => i.id == val).orgId
      getAction('/wz/doctor/wzDoctorInfo/list', { pageNo: 1, pageSize: 999, orgId: orgId }).then(res => {
        this.docList = res.result.records;
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>